package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.monitor.server.biz.domain.MonitorAlarmRulesEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorApplicationEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorItemEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorPluginEntity;
import com.glodon.paas.monitor.server.biz.domain.MonitorSendRulesEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorPluginService;
import com.glodon.paas.monitor.server.constants.MonitorStatusEnum;
import com.glodon.paas.monitor.server.dto.MonitorAlarmRulesDTO;
import com.glodon.paas.monitor.server.dto.MonitorApplicationUserDTO;
import com.glodon.paas.monitor.server.dto.MonitorItemDTO;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.dto.MonitorSendRulesDTO;
import com.glodon.paas.monitor.server.exposed.facade.MonitorAppUserFacadeService;
import com.glodon.paas.monitor.server.exposed.facade.MonitorItemAlarmSendFacadeService;
import com.glodon.paas.monitor.web.constants.MonitorWebPageConstants;

@Controller
public class MonitorSendController {

	@Autowired
	private MonitorItemAlarmSendFacadeService	monitorItemAlarmSendFacadeService;

	@Autowired
	private MonitorAppUserFacadeService			monitorAppUserFacadeService;

	@Autowired
	private MonitorPluginService				monitorPluginService;

	@RequestMapping(value = "sendMain", method = RequestMethod.GET)
	public ModelAndView indexMonitorSend(@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "alarmId", required = false) Long alarmId,
			@RequestParam(value = "sendType", required = false) String sendType,
			@RequestParam(value = "sendStatus", required = false) String sendStatus, ModelMap model,
			HttpServletRequest request) {
		boolean modelFlag = false;
		Long sendId = null;
		ModelMap doSendModel = (ModelMap) request.getSession().getAttribute("doSendModel");
		if (null != doSendModel && !doSendModel.isEmpty()) {
			modelFlag = true;
			sendId = (Long) doSendModel.get(StringConst.ITEM);
		}

		MonitorSendRulesDTO sendDto = new MonitorSendRulesDTO();
		if (null != appId) {
			sendDto.setAppId(appId);
		}

		if (null != pluginId) {
			sendDto.setPluginId(pluginId);
		}

		if (null != itemId) {
			sendDto.setItemId(itemId);
		}

		if (null != alarmId) {
			sendDto.setAlarmId(alarmId);
		}

		if (null != sendId) {
			sendDto.setId(sendId);
		}

		if (StringUtils.isNotBlank(sendType)) {
			sendDto.setSendType(sendType);
		}

		if (StringUtils.isNotBlank(sendStatus)) {
			sendDto.setSendStatus(sendStatus);
		}

		MonitorResultMsg<ArrayList<MonitorSendRulesEntity>> resultMsg = monitorItemAlarmSendFacadeService
				.queryMonitorSend(sendDto);

		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				ArrayList<MonitorSendRulesEntity> itemList = (ArrayList<MonitorSendRulesEntity>) resultMsg
						.getResultObject();
				List<MonitorSendRulesDTO> sendDTOList = new ArrayList<MonitorSendRulesDTO>();
				if (null != itemList && !itemList.isEmpty()) {
					for (MonitorSendRulesEntity entity : itemList) {
						MonitorSendRulesDTO sendRulesDTO = new MonitorSendRulesDTO();
						BeanUtils.copyProperties(entity, sendRulesDTO);
						MonitorResultMsg<MonitorApplicationUserDTO> appUserMsg = (MonitorResultMsg<MonitorApplicationUserDTO>) monitorAppUserFacadeService
								.findMonitorApplicationUser(entity.getAppId());
						if (null != appUserMsg && appUserMsg.isSuccess()) {
							MonitorApplicationUserDTO appUserDTO = (MonitorApplicationUserDTO) appUserMsg
									.getResultObject();
							MonitorApplicationEntity appEntity = appUserDTO.getMonitorApplicationEntity();
							if (null != appEntity) {
								sendRulesDTO.setAppName(appEntity.getAppName());
							}
						}
						MonitorPluginEntity pluginEntity = monitorPluginService
								.selectByPrimaryKey(entity.getPluginId());
						if (null != pluginEntity) {
							sendRulesDTO.setPluginName(pluginEntity.getPluginName());
						}
						MonitorResultMsg<MonitorItemEntity> itemMsg = (MonitorResultMsg<MonitorItemEntity>) monitorItemAlarmSendFacadeService
								.findMonitorItemById(entity.getItemId());
						if (null != itemMsg && itemMsg.isSuccess()) {
							MonitorItemEntity itemEntity = (MonitorItemEntity) itemMsg.getResultObject();
							if (null != itemEntity) {
								sendRulesDTO.setItemName(itemEntity.getItemName());
							}
						}

						MonitorResultMsg<MonitorAlarmRulesEntity> alarmMsg = monitorItemAlarmSendFacadeService
								.findMonitorAlarmRulesById(entity.getAlarmId());
						if (null != alarmMsg && alarmMsg.isSuccess()) {
							MonitorAlarmRulesEntity alarmEntity = (MonitorAlarmRulesEntity) alarmMsg.getResultObject();
							if (null != alarmEntity) {
								sendRulesDTO.setAlarmName(alarmEntity.getAlarmName());
							}
						}

						sendDTOList.add(sendRulesDTO);
					}
				}

				model.addAttribute(StringConst.ITEMS, sendDTOList);
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.QUERY_RESULT_EMPTY);
		}

		if (modelFlag) {
			model.addAttribute(StringConst.RETURN_MSG, doSendModel.get(StringConst.RETURN_MSG));
			model.addAttribute(StringConst.RESULT, doSendModel.get(StringConst.RESULT));
			request.getSession().removeAttribute("doSendModel");
		}
		return new ModelAndView("sendMain", model);
	}

	@RequestMapping(value = "sendAction", method = RequestMethod.GET)
	public ModelAndView indexSendAction(@RequestParam(value = "id", required = false) Long id, ModelMap model) {

		MonitorApplicationEntity entity = new MonitorApplicationEntity();
		MonitorResultMsg<ArrayList<MonitorApplicationEntity>> entityrReslut = monitorAppUserFacadeService
				.queryMonitorApplication(entity);
		if (null != entityrReslut) {
			if (entityrReslut.isSuccess()) {
				ArrayList<MonitorApplicationEntity> entityList = entityrReslut.getResultObject();
				model.addAttribute(MonitorWebPageConstants.APP_LIST, entityList);
			}
		}

		List<MonitorPluginEntity> pluginList = monitorPluginService.listAllPlugin();
		if (null != pluginList && !pluginList.isEmpty()) {
			model.addAttribute(MonitorWebPageConstants.PLUGIN_LIST, pluginList);
		}

		MonitorItemDTO itemEntity = new MonitorItemDTO();
		MonitorResultMsg<ArrayList<MonitorItemDTO>> itemResultMsg = monitorItemAlarmSendFacadeService
				.queryMonitorItem(itemEntity);
		if (null != itemResultMsg) {
			if (itemResultMsg.isSuccess()) {
				ArrayList<MonitorItemDTO> itemList = itemResultMsg.getResultObject();
				model.addAttribute(MonitorWebPageConstants.ITEM_LIST, itemList);
			}
		}

		MonitorAlarmRulesDTO monitorAlarmRulesDTO = new MonitorAlarmRulesDTO();
		MonitorResultMsg<ArrayList<MonitorAlarmRulesEntity>> alarmMsg = monitorItemAlarmSendFacadeService
				.queryMonitorAlarm(monitorAlarmRulesDTO);
		if (null != alarmMsg && alarmMsg.isSuccess()) {
			ArrayList<MonitorAlarmRulesEntity> alarmList = (ArrayList<MonitorAlarmRulesEntity>) alarmMsg
					.getResultObject();
			model.addAttribute(MonitorWebPageConstants.ALARM_LIST, alarmList);
		}

		MonitorResultMsg<MonitorSendRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
				.findMonitorSendRulesById(id);
		if (null != resultMsg && resultMsg.isSuccess()) {
			MonitorSendRulesEntity sendEntity = (MonitorSendRulesEntity) resultMsg.getResultObject();
			if (null != sendEntity) {
				model.addAttribute("sendEntity", sendEntity);
			}
		}

		model.addAttribute("sendId", id);
		return new ModelAndView("sendAction", model);
	}

	@RequestMapping(value = "doSendAction", method = RequestMethod.POST)
	public ModelAndView doAppCreate(@RequestParam(value = "sendId", required = false) Long sendId,
			@RequestParam(value = "appId", required = false) Long appId,
			@RequestParam(value = "pluginId", required = false) Long pluginId,
			@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "alarmId", required = false) Long alarmId,
			@RequestParam(value = "sendTime", required = false) String sendTime,
			@RequestParam(value = "sendType", required = false) String sendType,
			@RequestParam(value = "sendContent", required = false) String sendContent, ModelMap model,
			HttpServletRequest request) {
		if (null != sendId) {
			MonitorResultMsg<MonitorSendRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
					.findMonitorSendRulesById(sendId);
			if (null != resultMsg && resultMsg.isSuccess()) {
				MonitorSendRulesEntity sendRulesEntity = (MonitorSendRulesEntity) resultMsg.getResultObject();
				sendRulesEntity.setSendStatus(MonitorStatusEnum.OFF.getCode());
				sendRulesEntity.setUpdateTime(new Date());
				sendRulesEntity.setAlarmId(alarmId);
				sendRulesEntity.setAppId(appId);
				sendRulesEntity.setItemId(itemId);
				sendRulesEntity.setPluginId(pluginId);
				sendRulesEntity.setSendContent(sendContent);
				sendRulesEntity.setSendTime(new Date());
				sendRulesEntity.setSendType(sendType);
				MonitorResultMsg<MonitorSendRulesEntity> updateResultMsg = monitorItemAlarmSendFacadeService
						.updateMonitorSendRules(sendRulesEntity);
				model.addAttribute(StringConst.ITEM, sendId);
				if (null != updateResultMsg) {
					if (resultMsg.isSuccess()) {
						model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
						model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_SUCCESS);
					} else {
						model.addAttribute(StringConst.RESULT, StringConst.FAIL);
						model.addAttribute(StringConst.RETURN_MSG, updateResultMsg.getResultMsg());
					}
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.UPDATE_FAIL);
				}
			}
		} else {
			MonitorSendRulesEntity sendRulesEntity = new MonitorSendRulesEntity();
			sendRulesEntity.setSendStatus(MonitorStatusEnum.ON.getCode());
			sendRulesEntity.setCreateTime(new Date());
			sendRulesEntity.setAlarmId(alarmId);
			sendRulesEntity.setAppId(appId);
			sendRulesEntity.setItemId(itemId);
			sendRulesEntity.setPluginId(pluginId);
			sendRulesEntity.setSendContent(sendContent);
			sendRulesEntity.setSendTime(new Date());
			sendRulesEntity.setSendType(sendType);
			MonitorResultMsg<MonitorSendRulesEntity> resultMsg = monitorItemAlarmSendFacadeService
					.saveMonitorSendRules(sendRulesEntity);
			if (null != resultMsg) {
				if (resultMsg.isSuccess()) {
					MonitorSendRulesDTO monitorSendRulesDTO = new MonitorSendRulesDTO();
					monitorSendRulesDTO.setAlarmId(alarmId);
					monitorSendRulesDTO.setAppId(appId);
					monitorSendRulesDTO.setItemId(itemId);
					monitorSendRulesDTO.setPluginId(pluginId);
					monitorSendRulesDTO.setSendStatus(sendRulesEntity.getSendStatus());
					monitorSendRulesDTO.setSendType(sendType);
					MonitorResultMsg<ArrayList<MonitorSendRulesEntity>> sendMsg = monitorItemAlarmSendFacadeService
							.queryMonitorSend(monitorSendRulesDTO);
					if (null != sendMsg && sendMsg.isSuccess()) {
						ArrayList<MonitorSendRulesEntity> sendList = (ArrayList<MonitorSendRulesEntity>) sendMsg
								.getResultObject();
						if (null != sendList && !sendList.isEmpty()) {
							model.addAttribute(StringConst.ITEM, sendList.get(0).getId());
						}
					}
					model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
					model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_SUCCESS);
				} else {
					model.addAttribute(StringConst.RESULT, StringConst.FAIL);
					model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
				}
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CREATE_FAIL);
			}
		}
		request.getSession().setAttribute("doSendModel", model);
		return new ModelAndView(new RedirectView("sendMain"));
	}

	@RequestMapping(value = "deleteSend", method = RequestMethod.GET)
	public ModelAndView doSendDelete(@RequestParam("id") Long id, ModelMap model, HttpServletRequest request) {
		if (null == id) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doSendModel", model);
			return new ModelAndView(new RedirectView("sendMain"));
		}
		MonitorResultMsg<Boolean> resultMsg = monitorItemAlarmSendFacadeService.deleteMonitorSendRules(id);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.DELETE_FAIL);
		}
		request.getSession().setAttribute("doSendModel", model);
		return new ModelAndView(new RedirectView("sendMain"));
	}

	@RequestMapping(value = "changeStatusSend", method = RequestMethod.GET)
	public ModelAndView doSendChangeStatus(@RequestParam("id") Long id, @RequestParam("sendStatus") String sendStatus,
			ModelMap model, HttpServletRequest request) {
		if (null == id || StringUtils.isBlank(sendStatus)) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.INVALID_PARAMETER);
			request.getSession().setAttribute("doSendModel", model);
			return new ModelAndView(new RedirectView("sendMain"));
		}

		MonitorResultMsg<MonitorSendRulesEntity> resultMsg = monitorItemAlarmSendFacadeService.changeMonitorSendStatus(
				id, sendStatus);
		if (null != resultMsg) {
			if (resultMsg.isSuccess()) {
				model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
				model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_SUCCESS);
			} else {
				model.addAttribute(StringConst.RESULT, StringConst.FAIL);
				model.addAttribute(StringConst.RETURN_MSG, resultMsg.getResultMsg());
			}
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.CHANGE_FAIL);
		}
		model.addAttribute(StringConst.ITEM, id);
		request.getSession().setAttribute("doSendModel", model);
		return new ModelAndView(new RedirectView("sendMain"));
	}

}
